package capricornus.components.skill
{
	import capricornus.components.ComponentsManager;
	import capricornus.components.utils.movieclip.MovieImages;
	import capricornus.scene.AbstractScene;
	import capricornus.scene.SceneManager;
	import capricornus.scene.events.ActionSceneEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	public class SkillManager
	{
		private static var _ins:SkillManager = null;
		
		/**
		 * 场景 
		 */		
		private var _scene:AbstractScene;
		/**
		 * 技能层，这个是技能上层
		 */		
		private var _skillTopLayer:Sprite;
		/**
		 * 技能层，这个是技能下层
		 */	
		private var _skillBottomLayer:Sprite;
		
		public function SkillManager()
		{
			init();
		}
		
		public static function get instance():SkillManager
		{
			if( _ins==null )
			{
				_ins = new SkillManager();
			}
			return _ins;
		}
		
		private function init():void
		{
			_skillBottomLayer = _scene.skillBottomLayer;
			_skillTopLayer = _scene.skillTopLayer;
		}
		
		/**
		 * 初始化 
		 * 注：在SceneManager::initialize里面调用
		 */		
		public function initialize():void
		{
			_scene = SceneManager.instance.scene;
			
			SceneManager.instance.scene.addEventListener( ActionSceneEvent.LEAVE_SCENE, onLevelScene, false, 0, true );
			SceneManager.instance.scene.addEventListener( ActionSceneEvent.ACTION_SCENE_SCROLLED, onSceneScrollRectChange, false, 0, true );
		}
		
		private function onLevelScene( e:ActionSceneEvent ):void
		{
			var mm:MovieImages;
			while( _skillTopLayer.numChildren > 0 )
			{
				mm = _skillTopLayer.removeChildAt(0) as MovieImages;
				if( mm != null ) mm.destroy();
			}
			while( _skillBottomLayer.numChildren > 0 )
			{
				mm = _skillBottomLayer.removeChildAt(0) as MovieImages;
				if( mm != null ) mm.destroy();
			}
		}
		
		private function onSceneScrollRectChange( e:ActionSceneEvent ):void
		{
			var rect:Rectangle = _scene.scrollRect;
			var len:int = _skillTopLayer.numChildren;
			var i:int;
			var mm:MovieImages;
			for( i=len-1; i>-1; i-- )
			{
				mm = _skillTopLayer.getChildAt(i) as MovieImages;
				if( !rect.contains( mm.x, mm.y ) )
				{
					_skillTopLayer.removeChildAt(i);
					mm.destroy();
				}
			}
			len = _skillBottomLayer.numChildren;
			for( i=len-1; i>-1; i-- )
			{
				mm = _skillBottomLayer.getChildAt(i) as MovieImages;
				if( !rect.contains( mm.x, mm.y ) )
				{
					_skillBottomLayer.removeChildAt(i);
					mm.destroy();
				}
			}
		}
		
		/**
		 * 播放技能 
		 * @param skillID 技能id
		 * @param x 技能起始位置
		 * @param y
		 * @param parent 技能特效的播放层，默认null；如果为null则放置默认层级
		 * @param dir 技能方向
		 * @param toX 技能结束位置，飞行类技能才会有这个值
		 * @param toY
		 * 
		 */		
		public function play( skillID:String, x:int, y:int, dir:int, parent:DisplayObjectContainer=null, toX:int=0, toY:int=0 ):void
		{
			
		}
	}
}






